/* Elasticity0_3.do (STATA)
	Estimate demand elasticity for Equity.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Elasticity0_3, replace;

/* Define local and global variables */

do global;


/* Compute mean elasticity */

Elasticity 0 3;

local Melast = r(Melast);

/* Load data */

u "../3 Estimation/Estimation0", clear;

/* Compute gradient of mean elasticity wrt demand coefficients */

mata: Delast_b = J(0,1,.);

foreach coef of varlist b_* {;
	Elasticity 0 3 `coef';

	local Delast = r(Delast);

	mata: Delast_b = (Delast_b \ `Delast');
};

/* Load data */

u "../3 Estimation/Estimation0", clear;

/* Compute gradient of mean elasticity wrt predictability coefficients */

mata: Delast_c = J(0,1,.);

foreach coef of varlist c_Lmb c_Lrealfx {;
	Elasticity 0 3 `coef';

	local Delast = r(Delast);

	mata: Delast_c = (Delast_c \ `Delast');
};

/* Load estimated coefficients */

mata: mata matuse "../3 Estimation/Estimation0", replace;
mata: mata matuse "../3 Estimation/Prediction0";

/* Report mean elasticity */

di round(`Melast',.1);

/* Compute standard error by delta method */

mata: SEelast = sqrt(Delast_b'*Var_b3*Delast_b+Delast_c'*Var_c3*Delast_c);
mata: round(SEelast,.1);

log close;
